package com.codechuan.didi_web.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.codechuan.didi_web.entity.SysUserAccount;
import com.codechuan.didi_web.entity.SysUserConfig;
import com.codechuan.didi_web.entity.SysUserToken;
import com.codechuan.didi_web.mapper.SysUserAccountMapper;
import com.codechuan.didi_web.mapper.SysUserConfigMapper;
import com.codechuan.didi_web.mapper.SysUserTokenMapper;
import com.codechuan.didi_web.service.SysUserConfigService;
import com.codechuan.didi_web.utils.ResultUtils;
import com.codechuan.didi_web.vo.ResultVO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class SysUserConfigServiceImpl implements SysUserConfigService {
    @Resource
    private SysUserConfigMapper sysUserConfigMapper;

    @Resource
    private SysUserTokenMapper sysUserTokenMapper;


    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * @param sysUserConfig 用户配置实体类
     * @Deprecated 新增用户配置
     */
    @Override
    public void insertConfig(SysUserConfig sysUserConfig) {
        // 先删除在新增
        sysUserConfigMapper.deleteByPrimaryKey(sysUserConfig.getKeyNumber());
        sysUserConfigMapper.insert(sysUserConfig);
    }

    /**
     * @param account token
     * @return 返回查询到的配置
     * @Deprecated 获取用户配置
     */
    @Override
    public ResultVO<Object> getUserConfig(String account) {
        String tokenData = redisTemplate.opsForValue().get(account);
        JSONObject jsonObject = JSONObject.parseObject(tokenData);
        assert jsonObject != null;
        String keyNumber = jsonObject.getString("keyNumber");
        SysUserConfig sysUserConfig = sysUserConfigMapper.selectByPrimaryKey(keyNumber);
        if (sysUserConfig != null) {
            return ResultUtils.success(sysUserConfig);
        }
        return ResultUtils.error(500, "未获取到配置,开始抢单后下次自动获取这次配置.", null);
    }

    /**
     * @param account token
     * @return 返回查询到的数据
     * @Deprecated 获取主号token和按钮状态
     */
    @Override
    public Object getMainTokenAndButtonStatus(String account) {
        // 获取卡密/账号 和 按钮状态
        String tokenData = redisTemplate.opsForValue().get(account);
        JSONObject jsonObject = JSONObject.parseObject(tokenData);
        assert jsonObject != null;
        String keyNumber = jsonObject.getString("keyNumber");
        // 封装主号信息
        String mainToken = sysUserTokenMapper.selectMainTokenByAccount(keyNumber);
        HashMap<String, String> resultMap = new HashMap<>();
        resultMap.put("token", mainToken == null ? "请先设置主号" : mainToken.substring(1, 30) + "...");

        // 封装按钮信息
        String qdStatus = redisTemplate.opsForValue().get(keyNumber);
        if (qdStatus == null) {
            resultMap.put("buttonStatus", "开始抢单");
        } else {
            resultMap.put("buttonStatus", qdStatus.split(":")[1].equals("1") ? "停止抢单" : "开始抢单");
        }
        // 封装城市信息
        SysUserConfig sysUserConfig = sysUserConfigMapper.selectByPrimaryKey(keyNumber);
        if (sysUserConfig != null) {
            resultMap.put("cityId", String.valueOf(sysUserConfig.getCityId()));
            resultMap.put("cityName", String.valueOf(sysUserConfig.getCityName()));
            resultMap.put("lat", String.valueOf(sysUserConfig.getLat()));
            resultMap.put("lng", String.valueOf(sysUserConfig.getLng()));
        } else {
            resultMap.put("cityId", "");
            resultMap.put("cityName", "");
            resultMap.put("lat", "");
            resultMap.put("lng", "");
        }

        return ResultUtils.success(resultMap);
    }

    /**
     * @param account token
     * @return 返回查询到的数据
     * @Deprecated 获取用户的token列表
     */
    @Override
    public Object getUserTokenList(String account) {
        // 获取卡密/账号 和 按钮状态
        String tokenData = redisTemplate.opsForValue().get(account);
        JSONObject jsonObject = JSONObject.parseObject(tokenData);
        assert jsonObject != null;
        String keyNumber = jsonObject.getString("keyNumber");
        return sysUserTokenMapper.selectAllTokenByAccount(keyNumber);
    }

    /**
     * @param account token
     * @Deprecated 获取用户的token列表
     */
    @Override
    public Object updateAccountTokenSxNumber(String account,String token,String sxNumber) {
        // 获取卡密/账号 和 按钮状态
        String tokenData = redisTemplate.opsForValue().get(account);
        JSONObject jsonObject = JSONObject.parseObject(tokenData);
        assert jsonObject != null;
        String keyNumber = jsonObject.getString("keyNumber");
        sysUserTokenMapper.updateAccountTokenSxNumber(keyNumber,token,sxNumber);
        return null;
    }
}
